Repository navigation
feat(ui): let a text field look at a paste before it takes it - #76
Closed
xiaoyumuxi wants to merge 1 commit into
Closed
xiaoyumuxi wants to merge 1 commit into
xiaoyumuxi wants to merge 1 commit into
Conversation
A chat box that attaches what was copied (the files a file manager copied, a screenshot) had no way to see a paste before the field inserted the file names: TextPasteNoText reaches only an application that owns the editor through OnEvent, and a TextField or TextArea of a page never shows its editor's events to the application. TextInput.Paste is offered every paste into the field first, from the keyboard, the edit menu or the system's menu, with the text the paste would insert (folded into one line for a single-line field, as macOS hands it) or nothing when the clipboard has no text. Returning true takes the paste: the editor is left alone and the window paints again. A cut is not offered. Box.OnPaste declares it on a TextField or a TextArea of a page.
This was referenced Nov 28, 2026
Author
|
上游已以 vivaui#505(086247ad)合入同一改动,#78 把它同步进 fork main(7a73f1ba),合并后这几处文件与本 PR 逐字相同,关闭。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue:无(在 Lithe 的 Agent 对话框里发现:粘贴文件管理器复制的文件或截图时,应用没有机会把它们作为附件,只能看着文件名被当成文字贴进去)
上游 PR:vivaui#505(同一个提交,cherry-pick 到上游 main)
改了什么
TextField和TextArea新增Box.OnPaste(handler func(text string) bool)。每次往这个输入框粘贴,不管来自键盘(⌘V / Ctrl+V)、右键编辑菜单的“粘贴”还是系统菜单的“粘贴”,输入框接收之前都会先交给处理函数:true表示由处理函数接管:输入框内容不变、不进撤销历史,窗口重绘一次,让处理函数改动的页面状态(比如新加的附件)马上显示出来。false时照常粘贴,可以撤销。OnPaste时,行为和原来完全一样。文件管理器复制文件时,通常也会把文件名作为文字放进剪贴板,所以处理函数应该先问
ClipboardFiles,再看文字。文档里的示例就是这么写的。各层的改动
window/text_input.go:新增TextInput.Paste func(text string) bool字段和文档。app/painter.go(applyTextEvent):在单行折叠之后、交给Editor或OnEvent之前调用Paste。只对两类事件调用:带文字的TextPaste,以及TextPasteNoText;explicit的粘贴不调用。返回true时不改编辑器,并把Redraw置为真。base/field.go:Box.OnPaste。它只能用在页面上的TextField或TextArea上,不能在Memo里用,处理函数也不能为 nil;违反任意一条都会 panic,并说明原因。Page.fieldPaste。base/names.go:新增fieldEntry.paste。base/page.go:TextInput把输入框的处理函数交给原生文本输入。base/edit_menu.go:编辑菜单的“粘贴”也是先折叠单行、再交给处理函数,和键盘粘贴一致。cmd/viva/initfiles/AGENTS.md.txt:补一条.OnPaste的用法,TestTheGuideMentionsEveryMethod要求每个方法都写进指南。ui/field_paste_test.go:新增四个测试。TestTextAreaOnPasteTakesOrLeavesThePaste:覆盖文件被接管、图片作为空串交给处理函数、文字照常粘贴且能撤销、剪切不经过处理函数,以及接管后页面重绘。TestTextFieldOnPasteSeesTheTextTheFieldWouldTake:单行输入框拿到的是折叠后的文字。TestEditMenuPasteIsOfferedToOnPaste:编辑菜单的“粘贴”也会交给处理函数。TestOnPasteNeedsATextFieldAndAHandler:用在非输入框上,或处理函数为 nil 时会 panic。为什么
聊天类输入框要像 Claude Code、Codex CLI 那样,把粘贴进来的文件或截图作为附件,而不是把文件名当文字贴进去。之前应用拿不到“即将粘贴”的时机:
怎么验证的
在 macOS(Apple Silicon)上运行:
gofmt -l app base ui window:没有输出go vet ./app/ ./base/ ./ui/ ./window/;同样的 vet 在GOOS=windows和GOOS=linux下也跑了;GOOS=js GOARCH=wasm下 vet 了./base/ ./ui/ ./window/go test ./ui/ ./base/ ./app/ ./window/ ./components/ -count=1全部通过。新增的测试在没有这个钩子时会失败。cmd/viva的打包测试:本机工具链的默认部署目标和测试里的min_system不一致,属于环境问题,和本改动无关边界在哪
这是框架能力:粘贴是输入框的通用行为,“粘贴前先看一眼”不属于某个应用的流程。